package com.ly.hotel.spainterface.jg;

import android.app.FragmentManager;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.gson.Gson;
import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.spainterface.jg.adapter.MassageAdapter;
import com.ly.hotel.spainterface.jg.adapter.MassageItemAdapter;
import com.ly.hotel.spainterface.jg.bean.Massage;
import com.ly.hotel.spainterface.jg.bean.MassageBean;
import com.ly.hotel.spainterface.jg.bean.MassageItem;
import com.ly.hotel.spainterface.jg.bean.MassageItemCarBean;
import com.ly.hotel.spainterface.jg.bean.OnAddToBasketCompleted;
import com.ly.hotel.spainterface.jg.bean.OnGetHostAndTvInfoListener;
import com.ly.hotel.spainterface.jg.bean.OnHandlerShoppingCar;
import com.ly.hotel.spainterface.jg.bean.gson.dbfile.DbRead;
import com.ly.hotel.spainterface.jg.bean.gson.goods.Goods;
import com.ly.hotel.spainterface.jg.bean.gson.goods.GoodsResult;
import com.ly.hotel.spainterface.jg.bean.gson.goodscata.GoodsCata;
import com.ly.hotel.spainterface.jg.bean.gson.goodscata.GoodsCataResult;
import com.ly.hotel.spainterface.jg.bean.gson.plugin.AppConfig;
import com.ly.hotel.spainterface.jg.bean.gson.plugin.PluginConfigure;
import com.ly.hotel.spainterface.jg.bean.gson.plugin.XmlConfigure;
import com.ly.hotel.spainterface.jg.bean.gson.postJson.GoodsPara;
import com.ly.hotel.spainterface.jg.utils.AvoidDoubleClickedUtils;
import com.ly.hotel.spainterface.jg.utils.GetMachineInfoUtils;
import com.ly.hotel.spainterface.jg.utils.GsonUtils;
import com.ly.hotel.spainterface.jg.utils.OkHttpUtils;
import com.ly.hotel.spainterface.jg.view.CustomBasketDialog;
import com.ly.hotel.spainterface.jg.view.CustomErrorDialog;
import com.ly.hotel.spainterface.jg.view.CustomErrorDialogFragment;
import com.ly.hotel.spainterface.jg.view.CustomLoadingDialog;
import com.ly.hotel.spainterface.jg.view.CustomQueryBillDialog;
import com.ly.hotel.spainterface.jg.view.CustomSettingDialogFragment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class JgActivity extends BaseActivity implements OnAddToBasketCompleted,OnHandlerShoppingCar,OnGetHostAndTvInfoListener {

    private List<Massage> massageList;// 大类项目集合
    private List<MassageItem> massageItemList;// 小类项目集合
    private MassageAdapter massageAdapter;
    private static final String TAG = "JgActivity";
    private CustomLoadingDialog loadingDialog;// 自定义加载框
    private int[] judgeNum = {0,1,2,3};// 0：请求大类数据，1：请求小类数据,2:请求插件配置数据,3:请求隐藏的商品数据
    private RecyclerView massageItemRecyclerView;
    private MassageItemAdapter itemAdapter;
    private int massageCount;// 记录大类的排序
    private String serverIP = "";// 电视服务器ip
    private String serverPORT = "";// 电视服务器端口
    private String mac = "";// 电视mac地址
    private String categoryName = "";// 房间类型
    private String tvName = "";// 电视编号
    private String pluginVersion = "JGv1.0.0";//版本号
    private String cashierIP;// 收银服务器ip
    private String cashierPORT;// 收银服务器port
    private List<AppConfig> appConfigs = new ArrayList<>();
    private String[] showCategory;// 用于显示哪些消费大类的showCategory配置
    private int limitedGoodsNum = 0;// 限制购买的商品数量；
    private boolean isShowSettingButton = false;// 是否隐藏系统设置按钮
    private boolean isOnlySee = false;
    private RelativeLayout settingRelativeLayout;// 系统设置布局
    private String[] goodsHideNo;// 隐藏商品的编号数组
    private Button shoppingBasketNum;// 购物篮上显示数字的小圆圈
    private Map<MassageItem, Integer> massageItemMap = new HashMap<>();// 用来保存回调得到的购物数据
    private boolean isOpenShoppingBasket;// 用于是否进入了购物车
    private List<MassageItem> massageItems = new ArrayList<>();

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    String jsonString = msg.obj.toString();
                    if(msg.arg1 == judgeNum[0]){// 处理消费大类数据
                        Log.d(TAG,"大类json:" + jsonString);
                        GoodsCataResult goodsCataResult = GsonUtils.parseSimpleJson(jsonString,GoodsCataResult.class);
                        if(goodsCataResult.isSuccess()){
                            List<GoodsCata> goodsCatas = goodsCataResult.getResult().getGoodCatalog();
                            massageList = new ArrayList<>();
                            for(int i = 0;i < goodsCatas.size();i ++){
                                GoodsCata goodsCata = goodsCatas.get(i);
                                String catalogId = goodsCata.getCatalogId();
                                String goodsCatalogName = goodsCata.getGoodsCatalogName();
                                /*Log.d(TAG,goodsCata.get$id());
                                Log.d(TAG,goodsCata.getGoodsCatalogName());
                                Log.d(TAG,goodsCata.getCatalogId());
                                Log.d(TAG,goodsCata.getGoodsRemark());
                                Log.d(TAG,goodsCata.getParentId());
                                Log.d(TAG,"****************************************");*/
                                Massage massage = new Massage();
                                if(showCategory != null){
                                    for(int j = 0;j < showCategory.length;j ++){
                                        if(catalogId.equals(showCategory[j])){
                                            massage.setId(catalogId);
                                            massage.setName(goodsCatalogName);
                                            massageList.add(massage);
                                        }
                                    }
                                }else{
                                    massage.setId(catalogId);
                                    massage.setName(goodsCatalogName);
                                    massageList.add(massage);
                                }
                            }
                            Log.d(TAG,"大类数目：" + massageList.size());
                            initMassageItemListView();// 初始化项目列表
                        }

                    }else if(msg.arg1 == judgeNum[1]){// 处理消费小类数据
                        Log.d(TAG,"小类json：" + jsonString);
                        GoodsResult goodsResult = GsonUtils.parseSimpleJson(jsonString,GoodsResult.class);
                        if(goodsResult.isSuccess()){
                            List<Goods> goodses = goodsResult.getResult().getLstGoods();
                            massageItemList = new ArrayList<>();
                            for(int i = 0;i < goodses.size();i ++){
                                Goods goods = goodses.get(i);
                                /*Log.d(TAG,goods.getGoodsName());
                                Log.d(TAG,goods.getPrice());
                                Log.d(TAG,goods.getUnit());
                                Log.d(TAG,goods.getGoodsId());
                                Log.d(TAG,"--------------------------------------");*/
                                MassageItem massageItem = new MassageItem();
                                massageItem.setId(goods.getGoodsId());
                                massageItem.setName(goods.getGoodsName());
                                massageItem.setUnit(goods.getUnit());
                                massageItem.setPrice(Double.parseDouble(goods.getPrice()));
                                if(goodsHideNo != null){
                                    boolean isEquals = false;
                                    for(int j = 0;j < goodsHideNo.length;j ++){
                                        if(goodsHideNo[j].equals(massageItem.getId())){
                                            isEquals = true;
                                            break;
                                        }
                                    }
                                    if(! isEquals){
                                        massageItemList.add(massageItem);
                                    }
                                }else{
                                    massageItemList.add(massageItem);
                                }
                            }
                            if(loadingDialog != null){
                                loadingDialog.close();
                            }
                            showMassageItem();
                        }
                    }else if(msg.arg1 == judgeNum[2]){// 处理插件配置数据
                        PluginConfigure pluginConfigure = GsonUtils.parseSimpleJson(jsonString, PluginConfigure.class);
                        if(! pluginConfigure.getData().isEmpty()){
                            XmlConfigure xmlConfigure = GsonUtils.parseSimpleJson(pluginConfigure.getData(),XmlConfigure.class);
                            //Log.d(TAG,"插件配置:" + xmlConfigure.getConfigure());
                            parseXmlWithSAX(xmlConfigure.getConfigure());
                            getGoodsCataData();
                        }
                    }else if(msg.arg1 == judgeNum[3]){
                        DbRead dbRead = GsonUtils.parseSimpleJson(jsonString,DbRead.class);
                        if(dbRead.getCode() == 200){
                            String data = dbRead.getData();
                            Log.d("GoodsHideDialog","隐藏商品信息：" + data);
                            if(! TextUtils.isEmpty(data)){
                                goodsHideNo = data.split(" ");
                            }
                        }
                    }
                    break;

                case 2:
                    if(loadingDialog != null){
                        loadingDialog.close();
                    }
                    if(msg.arg2 == judgeNum[2]){
                        Log.d("JgActivity","无法获取插件配置数据");
                        showErrorDialogFragment("无法获取插件配置数据！");
                    }else if(msg.arg2 == judgeNum[3]){
                        Log.d("JgActivity","无法获取隐藏商品数据");
                    }else if(msg.arg2 == judgeNum[1]){
                        Log.d("JgActivity","无法商品小类信息");
                        showErrorDialogFragment("无法获取商品小类数据！");
                    }else if(msg.arg2 == judgeNum[0]){
                        Log.d("JgActivity","无法商品大类信息");
                        showErrorDialogFragment("无法获取商品大类数据！");
                    }
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jg_activity_main);
        loadingDialog = new CustomLoadingDialog(this,"加载中...");// 初始化自定义Dialog
        loadingDialog.show();
        getHostAndTvInfo();
        initTitle();
        initFunction();
    }

    // 获取电视mac和服务器ip、端口
    public void getHostAndTvInfo(){
        GetMachineInfoUtils getMachineInfo = new GetMachineInfoUtils();
        getMachineInfo.setOnGetHostAndTvInfoListener(this);
        getMachineInfo.getHostAndTvInof();
    }

    @Override
    public void getHostAndTvInfo(String serverIP, String serverPORT, String mac,String categoryName,String tvName) {
        Log.d(TAG,"serverIP=" + serverIP);
        Log.d(TAG,"serverPORT=" + serverPORT);
        Log.d(TAG,"mac=" + mac);
        Log.d(TAG,"categoryName=" + categoryName);
        Log.d(TAG,"tvName=" + tvName);

        this.serverIP = serverIP;
        this.serverPORT = serverPORT;
        this.mac = mac;
        this.categoryName = categoryName;
        this.tvName = tvName;
        getGoodsHide();
        getPluginConfiguration();
    }

    // 设置初始化的标题栏
    private void initTitle() {
        View view = findViewById(R.id.custom_title_layout);
        TextView titleName = (TextView) view.findViewById(R.id.activity_name);
        titleName.setText("消费");
        titleName.setTextColor(ContextCompat.getColor(this,R.color.mainWhite));
    }

    // 获得隐藏的商品文本
    public void getGoodsHide(){
        String url = "http://" + serverIP + ":" + serverPORT + "/api/dbfile-read.json?filename=JGGoodsHide&content=";
        OkHttpUtils.getInstance().getTextResource(url,handler,judgeNum[3]);
    }

    // 获得插件配置
    private void getPluginConfiguration(){
        String url = "http://" + serverIP + ":" + serverPORT + "/api/plugin.json";
        OkHttpUtils.getInstance().getTextResource(url,handler,judgeNum[2]);
    }

    // 初始化购物篮、账单查询、系统设置按钮
    private void initFunction(){
        ImageButton searchThearp = (ImageButton) findViewById(R.id.search_thearp);
        ImageButton systemSetting = (ImageButton) findViewById(R.id.system_setting);
        ImageButton shoppingBasket = (ImageButton) findViewById(R.id.shopping_basket);
        shoppingBasketNum = (Button) findViewById(R.id.shopping_basket_num);
        final ImageButton queryBill = (ImageButton) findViewById(R.id.query_bill);
        settingRelativeLayout = (RelativeLayout) findViewById(R.id.system_setting_relativelayout);

        // 查询技师
        searchThearp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AvoidDoubleClickedUtils.isNotDoubleClicked()){// 如果点击的间隔少于500毫秒
                    return;
                }
                Toast.makeText(JgActivity.this,"查询技师功能暂未开放！",Toast.LENGTH_SHORT).show();
            }
        });

        // 系统设置
        systemSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AvoidDoubleClickedUtils.isNotDoubleClicked()){// 如果点击的间隔少于500毫秒
                    return;
                }
                CustomSettingDialogFragment settingDialogFragment = new CustomSettingDialogFragment();
                settingDialogFragment.setCancelable(false);// 设置点击非Dialog不可关闭
                Bundle bundle = new Bundle();
                MassageBean massageBean = new MassageBean();
                massageBean.setMassageList(massageList);
                bundle.putSerializable("massageBean",massageBean);
                bundle.putString("serverIP",serverIP);
                bundle.putString("serverPORT",serverPORT);
                bundle.putString("cashierIP",cashierIP);
                bundle.putString("cashierPORT",cashierPORT);
                settingDialogFragment.setArguments(bundle);
                settingDialogFragment.show(getFragmentManager(),"SettingDialogFragment");
            }
        });

        // 购物篮
        shoppingBasket.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AvoidDoubleClickedUtils.isNotDoubleClicked()){// 如果点击的间隔少于500毫秒
                    return;
                }
                isOpenShoppingBasket = true;
                CustomBasketDialog basketDialog = new CustomBasketDialog();
                basketDialog.setCancelable(false);// 设置点击非Dialog不可关闭
                basketDialog.setOnHandlerShoppingCar(JgActivity.this);
                Bundle bundle = new Bundle();
                MassageItemCarBean itemCarBean = new MassageItemCarBean();
                itemCarBean.setMassageItems(massageItems);
                bundle.putSerializable("MassageItemCarBean",itemCarBean);
                bundle.putString("serverIP",serverIP);
                bundle.putString("serverPORT",serverPORT);
                bundle.putString("cashierIP",cashierIP);
                bundle.putString("cashierPORT",cashierPORT);
                bundle.putInt("limitedGoodsNum",limitedGoodsNum);
                bundle.putString("categoryName",categoryName);
                basketDialog.setArguments(bundle);
                basketDialog.show(getFragmentManager(),"CustomBasketDialog");
            }
        });

        // 账单查询
        queryBill.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AvoidDoubleClickedUtils.isNotDoubleClicked()){// 如果点击的间隔少于500毫秒
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString("cashierIP",cashierIP);
                bundle.putString("cashierPORT",cashierPORT);
                bundle.putString("tvName",tvName);
                bundle.putString("categoryName",categoryName);

                CustomQueryBillDialog queryBillDialog = new CustomQueryBillDialog();
                queryBillDialog.setCancelable(false);
                queryBillDialog.setArguments(bundle);
                queryBillDialog.show(getFragmentManager(),"CustomQueryBillDialog");
            }
        });

    }

    // 错误提示界面
    private void showErrorDialogFragment(String errorMsg){
        CustomErrorDialogFragment dialogFragment = new CustomErrorDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("errorMsg",errorMsg);
        dialogFragment.setArguments(bundle);
        dialogFragment.setCancelable(false);
        dialogFragment.show(getFragmentManager(),"ErrorDialogFragment");
    }

    // 解析插件配置
    private void parseXmlWithSAX(String xmlString){
        try{
            Log.d(TAG,"plugin=" + MyApplication.pluginCfg);
            Log.d(TAG,"interfaceVersion=" + MyApplication.interfaceVersion);

            try{
                DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
                DocumentBuilder dombuilder = domfac.newDocumentBuilder();
                Document doc = dombuilder.parse(new ByteArrayInputStream(MyApplication.pluginCfg.getBytes("utf-8")));
                NodeList obj = doc.getElementsByTagName(MyApplication.interfaceVersion);
                AppConfig appConfig = new AppConfig();
                appConfig.setVersion(MyApplication.interfaceVersion);
                for (int i = 0; i < obj.getLength(); i++) {
                    NodeList sysList = ((Element) obj.item(i)).getElementsByTagName("xiaofei");

                    if (sysList.getLength() >= 1) {
                        NodeList headList = ((Element) sysList.item(0)).getElementsByTagName("ip");
                        if (headList.getLength() >= 1 && headList.item(0) != null) {
                            try {
                                String cashierIP = headList.item(0).getFirstChild().getNodeValue().trim();
                                appConfig.setIp(cashierIP);
                                Log.i(TAG, "cashierIP=" + cashierIP);
                            } catch (NumberFormatException e) {
                                Toast.makeText(this, "ip配置有误！", Toast.LENGTH_SHORT).show();
                            }
                        }

                        headList = ((Element) sysList.item(0)).getElementsByTagName("port");
                        if (headList.getLength() >= 1 && headList.item(0) != null) {
                            try {
                                String cashierPORT = headList.item(0).getFirstChild().getNodeValue().trim();
                                appConfig.setPort(cashierPORT);
                                Log.i(TAG, "cashierPORT=" + cashierPORT);
                            } catch (NumberFormatException e) {
                                Toast.makeText(this, "cashierPORT配置有误！", Toast.LENGTH_SHORT).show();
                            }
                        }

                        headList = ((Element) sysList.item(0)).getElementsByTagName("showCategory");
                        if (headList.getLength() >= 1 && headList.item(0) != null) {
                            try {
                                String showCategory = headList.item(0).getFirstChild().getNodeValue().trim();
                                appConfig.setShowCategory(showCategory);
                                Log.i(TAG, "showCategory=" + showCategory);
                            } catch (NumberFormatException e) {
                                Toast.makeText(this, "showCategory配置有误！", Toast.LENGTH_SHORT).show();
                            }
                        }

                        headList = ((Element) sysList.item(0)).getElementsByTagName("limitedGoodsNum");
                        if (headList.getLength() >= 1 && headList.item(0) != null) {
                            try {
                                String limitedGoodsNum = headList.item(0).getFirstChild().getNodeValue().trim();
                                appConfig.setLimitedGoodsNum(limitedGoodsNum);
                                Log.i(TAG, "limitedGoodsNum=" + limitedGoodsNum);
                            } catch (NumberFormatException e) {
                                Toast.makeText(this, "limitedGoodsNum配置有误！", Toast.LENGTH_SHORT).show();
                            }
                        }

                        headList = ((Element) sysList.item(0)).getElementsByTagName("isShowSettingButton");
                        if (headList.getLength() >= 1 && headList.item(0) != null) {
                            try {
                                String isShowSettingButton = headList.item(0).getFirstChild().getNodeValue().trim();
                                appConfig.setIsShowSettingButton(isShowSettingButton);
                                Log.i(TAG, "isShowSettingButton=" + isShowSettingButton);
                            } catch (NumberFormatException e) {
                                Toast.makeText(this, "isShowSettingButton配置有误！", Toast.LENGTH_SHORT).show();
                            }
                        }

                        headList = ((Element) sysList.item(0)).getElementsByTagName("isOnlySee");
                        if (headList.getLength() >= 1 && headList.item(0) != null) {
                            try {
                                String isOnlySee = headList.item(0).getFirstChild().getNodeValue().trim();
                                appConfig.setIsOnlySee(isOnlySee);
                                Log.i(TAG, "isOnlySee=" + isOnlySee);
                            } catch (NumberFormatException e) {
                                Toast.makeText(this, "isOnlySee配置有误！", Toast.LENGTH_SHORT).show();
                            }
                        }
                        appConfigs.add(appConfig);
                    }
                }

            }catch (Exception e){
                e.printStackTrace();
            }


            /*SAXParserFactory factory = SAXParserFactory.newInstance();
            XMLReader reader = factory.newSAXParser().getXMLReader();
            XmlContentHandler contentHandler = new XmlContentHandler();
            reader.setContentHandler(contentHandler);
            reader.parse(new InputSource(new StringReader(xmlString)));
            appConfigs = contentHandler.getAppConfigList();*/
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    // 初始化大类项目列表
    private void initMassageItemListView(){
        ListView massageItemListView = (ListView) findViewById(R.id.massage_item_listview);
        massageItemListView.setVerticalScrollBarEnabled(false);//隐藏滚动条
        massageAdapter = new MassageAdapter(this,massageList,false);
        massageItemListView.setAdapter(massageAdapter);
        // 监听点击事件
        massageItemListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(loadingDialog != null){
                    loadingDialog.close();
                }
                massageCount = position;
                massageAdapter.setDefaultItem(position);// 点击设置选中项
                Massage massage = massageList.get(position);
                loadingDialog = new CustomLoadingDialog(JgActivity.this,"加载中...");
                loadingDialog.show();
                getGoods(massage.getId());// 获取小类数据
            }
        });
        // 默认选中第一项
        massageItemListView.performItemClick(massageItemListView.getChildAt(0),0,massageItemListView.getItemIdAtPosition(0));
    }

    // 请求消费大类的数据
    private void getGoodsCataData(){
        Map<String,String> map = new HashMap<>();
        map.put("","");
        String jsonString = "";// 提交的请求json字符串
        String url = "";
        if(appConfigs.size() == 0){
            CustomErrorDialog errorDialog = new CustomErrorDialog(this,"插件配置出错！");
            errorDialog.show();
        }else{
            for (int i = 0;i < appConfigs.size();i ++) {
                AppConfig appConfig = appConfigs.get(i);
                if(appConfig.getVersion().equals(pluginVersion)){
                    cashierIP = appConfig.getIp();
                    cashierPORT = appConfig.getPort();
                    url = "http://" + cashierIP + ":" + cashierPORT + "/TsInterface/Ts_GetCatalog";
                    if(!TextUtils.isEmpty(appConfig.getShowCategory())){
                        // 获取showCategory配置
                        showCategory = appConfig.getShowCategory().split(",");
                    }

                    try{
                        limitedGoodsNum = Integer.parseInt(appConfig.getLimitedGoodsNum());
                        isShowSettingButton = Boolean.parseBoolean(appConfig.getIsShowSettingButton());
                        isOnlySee = Boolean.parseBoolean(appConfig.getIsOnlySee());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
            if(url == ""){
                CustomErrorDialog errorDialog = new CustomErrorDialog(this,"插件版本不正确！");
                errorDialog.show();
            }else{
                Log.d(TAG,"url=" + url);
                // 请求大类数据
                OkHttpUtils.getInstance().postJsonResource(jsonString,url,handler, judgeNum[0]);
            }
        }
        Log.d(TAG,"limitedGoodsNum=" + limitedGoodsNum + " isShowSettingButton=" + isShowSettingButton +
                " isOnlySee=" + isOnlySee);
        if(isShowSettingButton){// 系统设置功能显示
            settingRelativeLayout.setVisibility(View.VISIBLE);
        }
    }

    // 请求消费小类的数据
    private void getGoods(String CatalogId){
        GoodsPara goodsPara = new GoodsPara();
        goodsPara.setCatalogId(CatalogId);
        Gson gson = new Gson();
        String jsonString = gson.toJson(goodsPara);// 提交的json数据
        String url = "";
        for (int i = 0;i < appConfigs.size();i ++) {
            AppConfig appConfig = appConfigs.get(i);
            if(appConfig.getVersion().equals(pluginVersion)){
                url = "http://" + appConfig.getIp() + ":" + appConfig.getPort() + "/TsInterface/Ts_GetGoods";
            }
        }
        OkHttpUtils.getInstance().postJsonResource(jsonString,url,handler,judgeNum[1]);
    }

    // 更新小类项目界面
    private void showMassageItem(){
        massageItemRecyclerView = (RecyclerView) findViewById(R.id.massage_recyclerview);
        GridLayoutManager layoutManager = new GridLayoutManager(this,3);
        FragmentManager fragmentManager = getFragmentManager();
        massageItemRecyclerView.setLayoutManager(layoutManager);
        itemAdapter = new MassageItemAdapter(massageItemList,massageItemRecyclerView,serverIP,
                serverPORT,fragmentManager,limitedGoodsNum,cashierIP,cashierPORT,tvName,
                categoryName,massageItems,isOnlySee);
        massageItemRecyclerView.setAdapter(itemAdapter);
        itemAdapter.setOnAddToBasketCompleted(this);
    }

    @Override
    public void addToBasketCompleted(MassageItem massageItem, int num) {
        //Log.d("JgActivity","添加到购物车的商品名称：" + massageItem.getName());
        //Log.d("JgActivity","添加到购物车的商品数量：" + num);
        if(massageItems.contains(massageItem)){
            Toast.makeText(this,"商品已加入到购物篮中",Toast.LENGTH_SHORT).show();
        }else{
            boolean isAddToCar = false;
            if(massageItems.size() > 0){
                for(int i = 0;i < massageItems.size();i ++) {
                    if (massageItems.get(i).getId().equals(massageItem.getId())) {
                        isAddToCar = true;
                        massageItems.get(i).setNum(massageItem.getNum());
                        break;
                    }
                }
                if(!isAddToCar){
                    massageItems.add(massageItem);
                }
            }else{
                massageItems.add(massageItem);
            }
            Toast.makeText(this,"商品成功加入到购物篮中",Toast.LENGTH_SHORT).show();
        }

        int goodsNum = 0;
        if(massageItems.size() > 0){
            for(int i = 0;i < massageItems.size();i ++){
                goodsNum += massageItems.get(i).getNum();
            }
        }
        shoppingBasketNum.setText(goodsNum + "");
    }

    @Override
    public void handlerShoppingCar(List<MassageItem> massageItems) {
        if(isOpenShoppingBasket){
            this.massageItems = null;
            this.massageItems = new ArrayList<>();
            this.massageItems = massageItems;
            int goodsNum = 0;
            if(this.massageItems.size() > 0){
                for(int i = 0;i < massageItems.size();i ++){
                    goodsNum += massageItems.get(i).getNum();
                }
                itemAdapter.notifyDataSetChanged();
            }
            itemAdapter.notifyDataSetChanged();
            shoppingBasketNum.setText(goodsNum + "");
            isOpenShoppingBasket = false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
